package com.andyage.interceptor;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.andyage.admin.bean.RoleMenuBean;
import com.andyage.admin.service.IRoleMenuService;

import net.sf.json.JSONObject;

public class MyInterceptor extends HandlerInterceptorAdapter {
	@Resource
	private IRoleMenuService iroleMenuService;

	/**
	 * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
	 * 
	 * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		super.afterConcurrentHandlingStarted(request, response, handler);
	}

	/**
	 * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 可在modelAndView中加入数据，比如传入视图的值
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		String menuId = request.getParameter("menu_id_user");
		String staffId = (String) request.getSession().getAttribute("AccountId");
		System.err.println(menuId);
		if (menuId != null && staffId != null) {
			RoleMenuBean roleMenu = new RoleMenuBean();
			roleMenu.setMenuId(Integer.valueOf(menuId));
			roleMenu.setStaffId(staffId);
			List<String> list = iroleMenuService.queryStaffRoleByStaffIdAndMenuId(roleMenu);
			modelAndView.addObject("list", list);
			System.out.println("postHandle里面出来的list=" + list);
		}
		super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
	 * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链,
	 * 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion()
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String user = (String) request.getSession().getAttribute("AccountId");
		String url = request.getRequestURL().toString();
		if (user == null) { // 判断用户是否存在，不存在返回登录界面，继续拦截，存在通过拦截，放行到访问页面
			if (request.getHeader("x-requested-with") != null
					&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
				// 如果是ajax请求响应头会有，x-requested-with
				JSONObject json = new JSONObject();
				if (url.contains("admin/AdStaffLogin.do")) {
					return true;
				} else {
					json.accumulate("result", 0);
					json.accumulate("tips", "用户身份已过期,请重新登录");
					response.setContentType("text/html;charset=UTF-8");
					response.getWriter().print(json);
					return false;
				}
				// response.setHeader("sessionstatus",
				// "timeout");//在响应头设置session状态
			} else {
				request.setAttribute("tips", "用户身份已过期,请重新登录");
				request.getRequestDispatcher("/adminLogin.jsp").forward(request, response);
				return false;
			}
		} else {
			return true;
		}
	}
}
